package com.rainner.app.server;

import java.sql.SQLException;
import java.util.List;

import com.rainner.app.javabean.BookBean;
import com.rainner.app.util.dbutil.BaseService;



public class BookServer extends BaseService<BookBean>{
	/**
	 * 返回书籍信息
	 * @param name
	 * @param value
	 * @param begin
	 * @return
	 * @throws SQLException
	 */
	public List<BookBean> allBookData(String name ,String value,int begin) throws SQLException{
		String sqlSelect;
		value = "%" + value + "%";
		if(name == null || value == null){
			sqlSelect = "select * from Book order by BookDate DESC limit ?,?";
			return super.query(sqlSelect,begin,begin + 5);
		}else{
			sqlSelect = "select * from Book where " + name + " like ? order by BookDate DESC limit ?,?";
			return super.query(sqlSelect,value,begin,begin + 5);
		}
		
	}
	/**
	 * 返回书籍总数
	 * @return
	 * @throws SQLException
	 */
	public long bookDataCount(String name,String value) throws SQLException{
		String sqlSelect;
		value = "%" + value + "%";
		if(name == null || value == null){
			sqlSelect = "select COUNT(1) from Book";
			return super.counts(sqlSelect);
		}else{
			sqlSelect = "select COUNT(1) from Book where " + name + " like ? ";
			return super.counts(sqlSelect,value);
		}
		
		
	}
	/**
	 * 通过bookId返回书籍信息
	 * @param bookId
	 * @return
	 * @throws SQLException
	 */
	public BookBean bookDataByBookId(String bookId) throws SQLException{
		String sqlSelect = "select * from Book where BookId = ?";
		return super.query(sqlSelect, bookId).get(0);
	}
	/**
	 * 减少书籍的数量
	 * @param bookId
	 * @param sumNumber
	 * @return
	 * @throws SQLException 
	 */
	public boolean subBookNumber(String bookId,int subNumber) throws SQLException{
		String sqlSelect = "select * from Book where BookId = ?";
		int number = super.query(sqlSelect, bookId).get(0).getBookNumber() - subNumber;
		String sqlUpdate = "update Book set BookNumber = ? where BookId = ?";
		return super.update(sqlUpdate, number,bookId);
	}
}
